package product;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;

import javax.swing.JFrame;
import javax.swing.Timer;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.util.Rotation;

public class SinglePieChart3D extends ApplicationFrame {

    public SinglePieChart3D(final String[][] temp) {
        super("선택된 제품들의 총재고 중 각 제품들의 비율");

        int total = 0;
        final DefaultPieDataset data = new DefaultPieDataset();
        for (int i = 0; i < temp.length; i++) {
				data.setValue(temp[i][0]+"", new Double(Integer.parseInt(temp[i][1]+"")));
				total += Integer.parseInt(temp[i][1]+"");
		} 

        final JFreeChart chart = ChartFactory.createPieChart3D(
            "Total : " + total,  // chart title 타이틀 폰트를 수정하려면 Jar파일 묶는 법 등을 알아야 함
            data,                   // data
            true,                   // include legend
            true,
            false
        );

		setDefaultCloseOperation(ApplicationFrame.HIDE_ON_CLOSE);
        chart.setBackgroundPaint(Color.yellow);
        final PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setStartAngle(270);
        plot.setDirection(Rotation.ANTICLOCKWISE);
        plot.setForegroundAlpha(0.75f); // 색의 진하기
        plot.setInteriorGap(0.18); // 보이는 각도
        // add the chart to a panel...
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);
        plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
                "{0} : {2}({1})", NumberFormat.getNumberInstance(), NumberFormat.getPercentInstance()
            ));
        
        final Rotator rotator = new Rotator(plot); 
        rotator.start();

    }
}

class Rotator extends Timer implements ActionListener {

    private PiePlot3D plot;

    private int angle = 270;

    Rotator(final PiePlot3D plot) {
        super(100, null);
        this.plot = plot;
        addActionListener(this);
    }

    public void actionPerformed(final ActionEvent event) {
        this.plot.setStartAngle(this.angle);
        this.angle = this.angle + 1;
        if (this.angle == 360) {
            this.angle = 0;
        }
    }

}
